package com.huaxiafinance.management.util;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaxia.middleware.sdk.utils.ServiceCodeAble;
import com.huaxiafinance.management.exception.BusinessException;
import org.apache.log4j.Logger;
import org.springframework.validation.BindingResult;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;


@JsonInclude(Include.NON_NULL)
public class MessageResult
{

    private static final boolean SUCCESS = true;

    private static final boolean FAULT = false;

    private static final Logger logger = Logger.getLogger(MessageResult.class);

    private boolean success = false;

    private String resultCode;

    private Object rows;

    private Object data;

    private Object parameter;

    private String msg;

    private BigDecimal sum;

    public MessageResult()
    {}

    public MessageResult(boolean success, boolean captchaEbabled)
    {
        this.success = success;
        this.data = captchaEbabled;
    }

    public void setSuccessResult()
    {
        this.success = true;
        setResultMessage(ServiceCode.SUCCESS);
    }

    public void setFailureResult()
    {
        this.success = false;
        setResultMessage(ServiceCode.EXCEPTION_ERROR);
    }

    public void setFailureResult(String msg)
    {
        this.setResultCode("5000");
        this.setMsg(msg);
        this.success = false;
    }

    public void setFailureResult(BusinessException be)
    {
        this.setResultCode(be.getCode());
        this.setMsg(be.getMessage());
        this.success = false;
    }

    public void setFailureResult(BindingResult bindingResult)
    {
        this.setResultCode(ServiceCode.PARAMETERS_ERROR.getCode());
        this.success = false;
        try
        {
            this.setMsg(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        catch (Exception e)
        {
            logger.error("错误++++++" + e);
            this.setMsg(ServiceCode.PARAMETERS_ERROR.getText());
        }
    }

    public void setResultMessage(ServiceCodeAble serviceCode)
    {
        this.setMsg(serviceCode.getText());
        this.setResultCode(serviceCode.getCode());
    }

    @JsonIgnore
    public String getAsJSON()
    {
        String json = null;
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mapper.setDateFormat(df);
            json = mapper.writeValueAsString(this);

        }
        catch (JsonMappingException e)
        {
            logger.error("错误++++++" + e);
            e.printStackTrace();
        }
        catch (JsonGenerationException e)
        {
            logger.error("错误++++++" + e);
            e.printStackTrace();
        }
        catch (IOException e)
        {
            logger.error("错误++++++" + e);
        }
        return json;
    }

    public boolean isSuccess()
    {
        return success;
    }

    public void setSuccess(boolean success)
    {
        this.success = success;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    public Object getRows()
    {
        return rows;
    }

    public void setRows(Object rows)
    {
        this.rows = rows;
    }

    public Object getData()
    {
        return data;
    }

    public void setData(Object data)
    {
        this.data = data;
    }

    public String getResultCode()
    {
        return resultCode;
    }

    public void setResultCode(String resultCode)
    {
        this.resultCode = resultCode;
    }

    public BigDecimal getSum()
    {
        return sum;
    }

    public void setSum(BigDecimal sum)
    {
        this.sum = sum;
    }

    public Object getParameter()
    {
        return parameter;
    }

    public void setParameter(Object parameter)
    {
        this.parameter = parameter;
    }
}
